package com.examlpe.lotterysystem.service.activitystatus.operater;

import com.examlpe.lotterysystem.dao.dataobject.ActivityPrizeDO;
import com.examlpe.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.examlpe.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.examlpe.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PriceOperater extends AbstractActivityOperater{

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPriceId();
        ActivityPrizeStatusEnum activityPrizeStatusEnum = convertActivityStatusDTO.getTargetPriceStatus();
        if(null == activityId ||null == prizeId || null == activityPrizeStatusEnum) {
            return false;
        }
        //判断奖品状态是否不是COMPLETE
        ActivityPrizeDO prizeDO = activityPrizeMapper.selectADId(activityId,prizeId);
        if(null == prizeDO) {
            return false;
        }
        //传入状态一致，不需要转换
        if (activityPrizeStatusEnum.name().equalsIgnoreCase(prizeDO.getStatus())) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try{
            activityPrizeMapper.updateStatus(convertActivityStatusDTO.getActivityId()
                    ,convertActivityStatusDTO.getPriceId()
                    ,convertActivityStatusDTO.getTargetPriceStatus().name());
            return true;
        }catch (Exception e) {
            return false;
        }

    }
}
